const App = {
  data() {
    return {
      party_organization_activities_members: {},
      pageTitle: "",
      Recorder: [],
      personal_information: [],
      activityType: [
        {
          value: "党员大会",
          label: "党员大会",
        },
        {
          value: "党委会",
          label: "党委会",
        },
        {
          value: "党小组会",
          label: "党小组会",
        },
        {
          value: "党课",
          label: "党课",
        },
        {
          value: "双联系双报道",
          label: "双联系双报道",
        },
        {
          value: "主题党日",
          label: "主题党日",
        },
      ],

      activityDevelop: [
        {
          value: "线上开展",
          label: "线上开展",
        },
        {
          value: "线下开展",
          label: "线下开展",
        },
      ],
      value: [], // 初始化 labelNumber 属性
      hjoptions: [], //省市区县
      Nation: [], //民族
      fileList: [], //身份证照片
    };
  },
  mounted() {
    let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
    console.log(TABLE_NAME);
    this.Data = TABLE_NAME;
    let type = sessionStorage.getItem("type");
    console.log(type);
    if (type == "Edit") {
      let key = sessionStorage.getItem("key");
      console.log(key);
      this.Edit(key);
    }
    this.Add();
    this.activities_members();
  },
  methods: {
    activities_members() {
      let lJSONObject_Data_Options = new Object();
      lJSONObject_Data_Options = JSON.parse(
        document.getElementById("MainTable").getAttribute("data-options")
      );
      let ls_MainTableName = lJSONObject_Data_Options.TableName;
      console.log(ls_MainTableName);
      const _this = this;
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryForIntiObject`,
        method: "get",
        data: {
          TableName: ls_MainTableName,
        },
        success: (res) => {
          console.log(res);
          _this.party_organization_activities_members = res[0];
          // console.log('  _this.grid_information', _this.grid_information);
        },
        error: (err) => {
          console.log(err);
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    //添加进来的方法   调党组织
    Add() {
      console.log();

      let condition = "Row_Status='0'";
      const _this = this;
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
        method: "post",
        data: {
          Data: "personal_information", //表名
          Condition: condition,
        },
        success: (res) => {
          console.log("res", res);
          _this.personal_information = res;
        },
        error: (err) => {
          console.log(err);
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
      });
      //查personal_information表的lD_Card_Number
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
        method: "post",
        data: {
          Data: "personal_information", //表名
          Condition: "Row_Status='0'",
        },
        success: (res) => {
          console.log("res", res);
          _this.Recorder = res;
          console.log(_this.Recorder);
        },
        error: (err) => {
          console.log(err);
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    //编辑进来方法
    Edit(key) {
      let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
      console.log(TABLE_NAME);
      let rowkey = sessionStorage.getItem("rowkey");
      console.log(rowkey);
      const _this = this;
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
        method: "post",
        data: {
          Data: TABLE_NAME, //表名
          Condition: `${key}='${rowkey}'`,
        },
        success: (res) => {
          console.log(res);
          _this.party_organization_activities = res[0];
          _this.value = [
            res[0].RR_Province_Number,
            res[0].RR_City_Number,
            res[0].RR_County_Nubmer,
            res[0].RR_Town_Number,
          ];
        },
        error: (err) => {
          console.log(err);
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    //提交表单
    handleSubmit() {
      let ls_URL = window.location.href;
      const Party_Organization_Activity_SerialNumber = getQueryObject(
        ls_URL,
        "Party_Organization_Activity_SerialNumber"
      ); // 获取 nodeKey 参数
      console.log(Party_Organization_Activity_SerialNumber);
      this.party_organization_activities_members.Party_Organization_Activity_SerialNumber =
        Party_Organization_Activity_SerialNumber;
      console.log(this.party_organization_activities_members);

      let type = sessionStorage.getItem("type");
      console.log(type);
      const appElement = document.getElementById("MainTable");
      console.log(appElement);
      let dataOptions = appElement.getAttribute("data-options");
      console.log(dataOptions);
      //动态获取到的表单数据
      let options = JSON.parse(dataOptions);
      console.log("options", options);
      let lArray = new Array();
      lArray.push(this[options.form]);
      //动态获取的表名
      let bm = options.TableName;
      console.log(bm);
      let lJSONObject = new Object();
      lJSONObject[bm] = lArray; //换表
      let lJSONObject_DataSet = new Object(); // Modify
      if (type == "Edit") {
        lJSONObject_DataSet.Modify = lJSONObject; //换表
      } else {
        lJSONObject_DataSet.Add = lJSONObject; //换表
      }
      console.log(lJSONObject_DataSet);

      this.AddApi(lJSONObject_DataSet);
    },
    AddApi(lJSONObject_DataSet) {
      console.log(lJSONObject_DataSet);
      const _this = this;
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/clientOperation/generalSaveForClient`,
        method: "post",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: (res) => {
          console.log(res);
          // 在普通 JavaScript 中使用 MessageBox
          ElMessage({
            message: "提交成功",
            type: "success",
            plain: true,
            duration: 1000, // 消息显示时长（毫秒）
          });
          // 等待 3 秒后回退页面
          setTimeout(() => {
            window.history.back();
          }, 1000);
        },
        error: (err) => {
          console.log(err);
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    handleChange(file, fileList) {
      const reader = new FileReader();
      reader.readAsDataURL(file.raw);
      reader.onload = () => {
        const base64Data = reader.result;
        console.log("Base64 编码的图片数据：", base64Data);
        const parts = base64Data.split("base64,");
        if (parts.length > 1) {
          var extractedData = parts[1];
          console.log(extractedData); //想要的数据
        } else {
          console.log("未找到 Base64 数据");
        }
        // return
        const _this = this;
        $.ajax({
          headers: {
            "content-type": "application/json",
          },
          url: `${mh}/clientOperation/parseIDCard`,
          method: "post",
          data: extractedData,
          success: (res) => {
            console.log(res);
            console.log(res.words_result[0].card_result);
            _this.personal_information.ID_Card_Number =
              res.words_result[0].card_result.公民身份号码.words;
            _this.personal_information.Personal_Name =
              res.words_result[0].card_result.姓名.words;
            _this.personal_information.Gender =
              res.words_result[0].card_result.性别.words;
            _this.personal_information.Registered_Residence =
              res.words_result[0].card_result.住址.words;
            _this.personal_information.Nation =
              res.words_result[0].card_result.民族.words;
            const birthStr = res.words_result[0].card_result.出生.words;
            if (birthStr.length === 8) {
              const formattedBirthday = `${birthStr.slice(
                0,
                4
              )}-${birthStr.slice(4, 6)}-${birthStr.slice(6)}`;
              _this.personal_information.Birthday = formattedBirthday;
            } else {
              console.log("出生日期格式不正确，应为 8 位数字");
            }
          },
          error: (err) => {
            console.log(err);
          },
          complete: (XMLHttpRequest) => {
            requestPretreatment(XMLHttpRequest);
          },
        });
      };
      reader.onerror = () => {
        console.error("读取文件时出错");
      };
    },
    goBack() {
      window.history.back();
    },
  },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus,{locale: zhCn});
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
